from datetime import date
from  flask import Flask,request,jsonify
import json

from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS

app = Flask(__name__)
app.config.from_object(Config)

db = SQLAlchemy(app)
CORS(app)
class Order(db.Model):
    __tablename__='order'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(20))
    price = db.Column(db.Integer,default=0)

# db.create_all()

@app.route('/a/add/',methods=['POST','GET'])
def add():
    if request.method=='POST':
        date = json.loads(request.data)
        name = date['name']
        price = date['price'] 
        order = Order(name=name,price=price)
        db.session.add(order)
        db.session.commit()
        return 'ok'
    else:
        ser = Order.query.all()
        list=[]
        for i in ser:
            dict={}
            dict['id']=i.id
            dict['price']=i.price
            dict['name']=i.name
            list.append(dict)
            
        return jsonify({'code':200,'list':list})

@app.route('/a/delete/',methods=['DELETE'])
def delete():
    if request.method==['DELETE']:
        id = request.args.get('id')
        print(id)
        Order.query.filter_by(id=id).delete()
        db.session.commit()
        return 'ok'


if __name__ == '__main__':
    app.run()

